package jzqb.dao.impl;

import cnki.tpi.base.DataContext;
import cnki.tpi.kbatis.sqlsession.SqlSession;
import cnki.tpi.kbatis.utils.DataSourceUtil;
import haha.life.common.SysTool;
import haha.life.normal.FileUtil;
import haha.life.normal.FileuploadUtils;
import jzqb.bo.QBBKBo;
import jzqb.bo.StandKnowleageBo;
import jzqb.dao.QBBKDao;
import jzqb.po.AdminUserPo;
import jzqb.po.AuthorityGroupPo;
import jzqb.po.QBBKPo;
import jzqb.po.QBBKTemplatePo;
import jzqb.vo.QBBKVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 情报百科数据层实现类
 */
@Repository
public class QBBKDaoImpl implements QBBKDao {

    @Autowired
    private DataContext dataContext;

    //封面存放临时路径
    @Value("${filePath.coverTemPResource}")
    private String coverTemPResource;
    //情报百科封面存放路径
    @Value("${filePath.qbbkCoverPath}")
    private String qbbkCoverPath;


    @Override
    public List<QBBKVo> showQBBKList(QBBKBo qbbkBo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.showQBBKList";
        return sqlSession.selectList(statementId, qbbkBo, false);
    }

    @Override
    public long showQBBKCount(QBBKBo qbbkBo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.showQBBKCount";
        return sqlSession.getCount(statementId, qbbkBo);
    }

    @Override
    public QBBKVo findQBBKBySysId(QBBKPo qbbkPo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.findQBBKBySysId";
        return sqlSession.selectOne(statementId, qbbkPo, false);
    }

    @Override
    public Boolean addQBBK(QBBKPo qbbkPo) throws UnsupportedEncodingException {
        //1 写入数据到数据库
        String recStr = FileUtil.generateQBBKRecFile("add", qbbkPo);
        String tableName = SysTool.QBBK_METADATA;
        boolean insetRes = dataContext.insertRecData(tableName, recStr);
        if (insetRes) { //写入成功
            String coverPath = qbbkPo.getCover();
            //将文件从临时目录挪动到正式目录
            if (!"".equals(coverPath) && coverPath != null) {
                String fromPath = coverTemPResource + "\\" + coverPath;
                String toPath = qbbkCoverPath + "\\" + coverPath;
                try {
                    FileuploadUtils.fileMove(fromPath, toPath);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return insetRes;
    }

    @Override
    public Boolean updateQBBK(QBBKPo qbbkPo) throws UnsupportedEncodingException {
        //1 写入数据到数据库
        String recStr = FileUtil.generateQBBKRecFile("edit", qbbkPo);
        String tableName = SysTool.QBBK_METADATA;
        boolean updateRes = dataContext.updateRecData(SysTool.QBBK_SYSID, tableName, recStr);
        return updateRes;
    }


    public synchronized Integer getQBBKMaxId(QBBKPo qbbkPo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        return sqlSession.getMaxId("sys_fld_sysid", "JZQBBK_METADATA");
    }


    @Override
    public List<StandKnowleageBo> getFieldNumByFlag(QBBKBo qbbkBo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.getFieldNumByFlag";
        return sqlSession.selectList(statementId, qbbkBo, false);
    }

    @Override
    public List<StandKnowleageBo> getDiffStatusQBBKNum(QBBKBo qbbkBo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.getDiffStatusQBBKNum";
        return sqlSession.selectList(statementId, qbbkBo, false);
    }

    @Override
    public QBBKPo getQBBKDetail(QBBKPo qbbkPo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.getQBBKDetail";
        return sqlSession.selectOne(statementId, qbbkPo, false);
    }

    @Override
    public Boolean updateQBBKStatus(QBBKPo qbbkPo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.updateQBBKStatus";
        return sqlSession.executeUpdate(statementId, qbbkPo);
    }

    @Override
    public Boolean recallQBBK(QBBKPo qbbkPo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.recallQBBK";
        return sqlSession.executeUpdate(statementId, qbbkPo);
    }

    @Override
    public Boolean deleteQBBK(QBBKPo qbbkPo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.deleteQBBK";
        return sqlSession.executeDelete(statementId, qbbkPo);
    }

    @Override
    public AuthorityGroupPo getUserMemberAndDeptMember(Integer AuditGroupId) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.getUserMemberAndDeptMember";
        return sqlSession.selectOne(statementId, AuditGroupId, false);
    }

    @Override
    public List<AdminUserPo> getUserByDepartBelongToSql(String departBelongToSql) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.getUserByDepartBelongToSql";
        return sqlSession.selectList(statementId, departBelongToSql, false);
    }

    @Override
    public List<AdminUserPo> getUserByUserBelongToSql(String userBelongToSql) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.getUserByUserBelongToSql";
        return sqlSession.selectList(statementId, userBelongToSql, false);
    }

    @Override
    public Boolean addQBBKTemplate(QBBKTemplatePo qbbkTemplatePo) throws UnsupportedEncodingException {
        //1 写入数据到数据库
        String recStr = FileUtil.generateQBBKTemplateREC("add", qbbkTemplatePo);
        String tableName = SysTool.YQTQBBKMB;
        boolean insetRes = dataContext.insertRecData(tableName, recStr);
        return insetRes;
    }

    @Override
    public Boolean updateQBBKTemplate(QBBKTemplatePo qbbkTemplatePo) throws UnsupportedEncodingException {
        //1 写入数据到数据库
        String recStr = FileUtil.generateQBBKTemplateREC("edit", qbbkTemplatePo);
        String tableName = SysTool.YQTQBBKMB;
        boolean updateRes = dataContext.updateRecData("id", tableName, recStr);
        return updateRes;
    }

    @Override
    public List<QBBKTemplatePo> showTemplateList() {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.showTemplateList";
        return sqlSession.selectList(statementId, null, false);
    }

    @Override
    public Boolean delQBBKTemplate(Integer id) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.delQBBKTemplate";
        return sqlSession.executeDelete(statementId, id);
    }

    @Override
    public List<String> findAllTemplateName() {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.QBBKDao.findAllTemplateName";
        return sqlSession.selectList(statementId, null, false);
    }
}
